package servlet.order;

import util.DbUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/InsertGoodsServlet")
@MultipartConfig
public class InsertGoodsServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String goodspic = session.getAttribute("zp").toString();
        String userid = session.getAttribute("zhi").toString();
        String goodsprice = req.getParameter("goods_price");
        int num = Integer.parseInt(req.getParameter("num"));
        String address = req.getParameter("address");
        String addressname = req.getParameter("address_name");
        String phone = req.getParameter("phone");
        DbUtil db = new DbUtil();
        String sql = "insert into orders(user_id,goods_pic,goods_price,num,address,address_name,phone) values('"+userid+"','"+goodspic+"','"+goodsprice+"',"+num+",'"+address+"','"+addressname+"','"+phone+"')";
        db.executeUpdate(sql);
        System.out.println(sql);
        resp.sendRedirect("GotoOrderServlet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
